package com.hyg.base.ui.base;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.IdRes;

import com.github.nukc.stateview.StateView;
import com.hyg.base.R;
import com.hyg.base.ui.common.FragmentContainerActivity;
import com.hyg.base.view.BaseView;
import com.hyg.common.utils.ToastUtils;
import com.hyg.common.view.dialog.ConfirmDialog;
import com.hyg.common.view.dialog.DialogUtils;
import com.hyg.idea.utils.RxUtil;

import io.reactivex.ObservableTransformer;

/**
 * @author huangyuguang
 * @description: Fragment的基类
 * @date 2018/5/3  17:09
 */

public abstract class BaseFragment<T extends BasePresenter> extends LazyLoadFragment implements BaseView {
    protected T mPresenter;
    private View rootView;
    protected StateView mStateView;//用于显示加载中、网络异常，空布局、内容布局
    protected Activity mActivity;
    private DialogUtils mDialogUtils;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mPresenter = createPresenter();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(provideContentViewId(),container,false);
            bindButterKnife(rootView);

            mStateView = StateView.inject(getStateViewRoot());
            if (mStateView != null){
                mStateView.setLoadingResource(R.layout.comm_lay_loading);
                mStateView.setRetryResource(R.layout.comm_lay_loadfailed);
                mStateView.setEmptyResource(R.layout.comm_lay_emptyview);
            }

        } else {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }

    /**
     * 标题
     */
    public String initTitle() {
        return null;
    }

    /**StateView的根布局，默认是整个界面，如果需要变换可以重写此方法*/
    public View getStateViewRoot() {
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
    }

    protected void showConfirmDialog(int stringResId) {
        showConfirmDialog(getString(stringResId));
    }

    protected void showConfirmDialog(String message) {
        new ConfirmDialog(getContext(),message,getString(R.string.lib_sure),null,null).show();
    }

    protected void showToast(String msg) {
        ToastUtils.show(msg);
    }

    protected void showLoading(){
        showLoading("");
    }

    protected void showLoading(boolean cancelTouchOutside){
        showLoading("",cancelTouchOutside);
    }

    protected void showLoading(String msg){
        showLoading(msg,true);
    }

    protected void showLoading(String msg,boolean cancelTouchOutside){
        if(mDialogUtils == null){
            mDialogUtils = new DialogUtils();
        }
        mDialogUtils.showProgress(getContext(),msg, cancelTouchOutside);
    }

    protected void hideLoading(){
        if(mDialogUtils != null){
            mDialogUtils.dismissProgress();
        }
    }

    /**
     * 该方法只适用于FragmentContainerActivity为容器的fragment
     * 也可以采用 initTitle() 方法初始化标题
     */
    protected void setNavBarTitle(String title){
        if(getActivity() instanceof FragmentContainerActivity){
            ((FragmentContainerActivity)getActivity()).setNavBarTitle(title);
        }
    }

    /**
     * 初始化标题栏其他内容
     */
    protected void initTitleView(View v){}

    protected <T extends View> T  findViewById(@IdRes int resId){
        return rootView.findViewById(resId);
    }

    protected void setOnClickListener(int resId,View.OnClickListener onClickListener){
        findViewById(resId).setOnClickListener(onClickListener);
    }

    protected void setText(int resId, String str){
        TextView tv = findViewById(resId);
        if(tv != null){
            tv.setText(str);
        }
    }

    protected void setVisibility(int resId, int visibility){
        View view = findViewById(resId);
        if(view != null){
            view.setVisibility(visibility);
        }
    }

    protected void bindButterKnife(View rootView){}

    @Override
    protected void onFragmentFirstVisible() {
        //当第一次可见的时候，加载数据
        initViewAndData();
    }

    @Override
    public <T> ObservableTransformer<T, T> rxSchedulerHelper(boolean showLoading) {
        return RxUtil.rxSchedulerHelper(this, showLoading);
    }

    /**
     * 重新设置新的FragmentContainerActivity的ContentView
     */
    public int setNewFragmentContainerView() {
        return R.layout.comm_fragment_container;
    }

    //得到当前界面的布局文件id(由子类实现)
    protected abstract int provideContentViewId();

    /**
     * 用于创建Presenter和判断是否使用MVP模式(由子类实现)
     */
    protected abstract T createPresenter();

    /**
     * 初始化页面数据
     */
    protected abstract void initViewAndData();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
        rootView = null;
    }

}
